
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:cc="http://java.sun.com/jsf/composite"
	xmlns:p="http://primefaces.org/ui"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:s="http://www.sensap.eu/jsf"
	xmlns:c="http://java.sun.com/jsp/jstl/core"
	xmlns:pe="http://primefaces.org/ui/extensions">

<!-- INTERFACE -->
<cc:interface>
	<cc:attribute name="id" />
</cc:interface>

<!-- IMPLEMENTATION -->
<cc:implementation>
	<p:dialog header="#{messages.UI_FIND_SOURCE_DIALOG_HEADER}"
		modal="true" closable="false" closeOnEscape="true" resizable="false"
		widgetVar="#{cc.attrs.id}">
		<h:form id="findSensorDialogForm">

			<p:gmap
				center="#{findSensorDialogController.context.mapCenter.lat},#{findSensorDialogController.context.mapCenter.lng}"
				type="HYBRID" model="#{findSensorDialogController.context.mapModel}"
				zoom="#{findSensorDialogController.context.mapZoom}"
				style="margin-top:5px;width:600px;height:400px;"
				styleClass="ui-corner-all" widgetVar="sensorMap"
				onPointClick="onPointClicked(event);">
				<p:ajax event="stateChange"
					listener="#{findSensorDialogController.onStateChange}" />
				<p:ajax event="pointSelect"
					listener="#{findSensorDialogController.onPointSelect}" />
			</p:gmap>

			<p:panelGrid columns="2" style="width:100%;box-sizing:border-box;"
				columnClasses="half-available-width">

				<p:outputPanel>
					<p:outputLabel for="searchLocation"
						value="#{messages.UI_FIND_SOURCE_DIALOG_LOCATION}" />
					<p:spacer width="10" />
					<p:inputText id="searchLocation"
						value="#{findSensorDialogController.context.searchLocation}"
						widgetVar="searchLocation" />
					<p:spacer width="10" />
					<p:commandButton
						value="#{messages.UI_FIND_SOURCE_DIALOG_LOCATION_FIND}"
						process="searchLocation"
						onsuccess="lookupAddress(searchLocation.jq.val());" />
				</p:outputPanel>

				<p:outputPanel>
					<p:outputLabel for="searchRadius"
						value="#{messages.UI_FIND_SOURCE_DIALOG_LOCATION_RADIUS}" />
					<p:spacer width="10" />
					<pe:inputNumber id="searchRadius" required="true"
						requiredMessage="#{messages.UI_FIND_SOURCE_DIALOG_LOCATION_RADIUS_KM} : #{messages.UI_VALIDATION_FIELD_REQUIRED}"
						value="#{findSensorDialogController.context.searchRadius}"
						symbolPosition="suffix"
						symbol="#{messages.UI_FIND_SOURCE_DIALOG_LOCATION_RADIUS_KM}" />
				</p:outputPanel>
			</p:panelGrid>

			<p:outputPanel layout="block" styleClass="ui-dialog-footer"
				style="text-align:right;">
				<p:separator />

				<p:commandButton id="findSensorsButton"
					value="#{messages.UI_FIND_SOURCE_DIALOG_RUN_SEARCH}"
					process="@form" 
					update="searchRadius, :toolboxForm, :nodeGraphForm"
					actionListener="#{applicationDesignPageController.executeSensorDiscoveryQuery}"
					onstart="windowBlockUI.block();#{cc.attrs.id}.hide();"
					oncomplete="windowBlockUI.unblock();if( args &amp;&amp; (args.notValid || args.validationFailed ) ){#{cc.attrs.id}.show();return false;}#{cc.attrs.id}.hide();return false;" />

				<p:button value="#{messages.DIALOG_CANCEL}"
					style="margin-left:10px;"
					onclick="#{cc.attrs.id}.hide();return false;" />
			</p:outputPanel>
		</h:form>
	</p:dialog>
	<script type="text/javascript">
            function onPointClicked(event){
                sensorMap.cfg.markers[0].setPosition(event.latLng);
            }
            
            function lookupAddress(address){
                var geocoder = new google.maps.Geocoder();
                if( geocoder ){
                    windowBlockUI.block();
                    geocoder.geocode({ address : address}, function(results, status) {
                        windowBlockUI.unblock();
                        if (status != google.maps.GeocoderStatus.OK) {
                            growl.renderMessage({
                                summary : '',
                                detail : '#{messages.UI_FIND_SOURCE_DIALOG_ADDRESS_NOT_FOUND}',
                                serverity : 0
                            });
                            return;
                        }
                        
                        sensorMap.map.setCenter(results[0].geometry.location);
                        sensorMap.fireStateChangeEvent();
                    });
                }
            }
        </script>
</cc:implementation>
</html>
